import os
import subprocess
import sys


def pipleline(inputNvm, inputN, workpath):
    NvmToMvs(inputN,workpath)
    DensifyPointCloud(workpath)
    ReconstructMesh(workpath)
    RefineMesh(workpath)
    TextureMesh(workpath)
    Viewer(workpath)

def NvmToMvs(inputN,workpath):
    print("1. Nvm To Mvs")
    pFeatures = subprocess.Popen([os.path.join(
       "InterfaceVisualSFM"),  "-i", inputN, "-o", "scene.mvs", "-w", workpath])
    pFeatures.wait()


def DensifyPointCloud(workpath):
    print("2. Densify point cloud")
    pFeatures = subprocess.Popen(
        [os.path.join("DensifyPointCloud"),  "scene.mvs", "-w", workpath])
    pFeatures.wait()


def ReconstructMesh(workpath):
    print("3. ReconstructMesh")
    pFeatures = subprocess.Popen(
        [os.path.join("ReconstructMesh"),  "scene_dense.mvs", "-w", workpath])
    pFeatures.wait()


def RefineMesh(workpath):
    print("4. RefineMesh")
    pFeatures = subprocess.Popen(
        [os.path.join("RefineMesh"),  "scene_dense_mesh.mvs", "-w", workpath])
    pFeatures.wait()


def TextureMesh(workpath):
    print("5. TextureMesh")
    pFeatures = subprocess.Popen(
        [os.path.join("TextureMesh"),  "scene_dense_mesh_refine.mvs", "-w", workpath])
    pFeatures.wait()


def Viewer(workpath):
    print("6. Viewer")
    pFeatures = subprocess.Popen(
        [os.path.join("Viewer"),  "scene_dense_mesh_refine_texture.mvs", "-w", workpath])
    pFeatures.wait()

def main():
    if len(sys.argv) < 2:
            print('''    ================================================
    ===    usage:python3 to run                  ===
    ===         workspace Nvm                    ===
    ===    example:                              ===
    ===          py  mvs.py D:/photo mvs.nvm     ===
    ================================================''')
    else:
            workpath = sys.argv[1]
            inputNvm = sys.argv[2]
            inputN = os.path.join(workpath,inputNvm)
            print(inputN)
            pipleline(inputNvm, inputN, workpath)

if __name__ == '__main__':
    main()
